package ride

import (
	"fmt"
	"log"
	"regexp"
	"strconv"
)

// Receipt 收据项目
type Receipt struct {
	Line string
}

// ChargeLine 输出账单
func (r Receipt) ChargeLine() string {
	return fmt.Sprintf("收费%d元", r.toRide().Price())
}

func (r Receipt) toRide() Ride {
	reg := regexp.MustCompile(`([0-9]+)公里,等待([0-9]+)分钟`)
	if reg == nil {
		log.Fatal("MustCompile err: " + r.Line)
	}

	matcher := reg.FindAllStringSubmatch(r.Line, -1)
	return Ride{Distance{parse(matcher[0][1])}, parse(matcher[0][2])}
}

func parse(v string) int {
	result, err := strconv.Atoi(v)
	if err != nil {
		log.Fatal(err)
	}
	return result
}
